package com.aia.ilp.web.json;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.util.StringUtils;

import com.aia.ann.domain.Agent;
import com.aia.ann.service.ChiefService;
import com.cfh.ahead.Constant;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerLoginJson extends ActionSupport implements SessionAware {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CustomerLoginJson.class);

	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	private String imageCode;
	private String co;
	private Map session;
	private ChiefService chiefService;

	public void validate() {
		try {
			{// 较验验证码
				String imgCode = (String) session.get(Constant.CAPTCHA_SESSION_HOLDER);
				if (!StringUtils.hasText(imgCode)) {
					addFieldError("imageCode", "请再次获取图片验证码");
					return;
				}

				if (!StringUtils.hasText(imageCode)) {
					addFieldError("imageCode", "请输入图片验证码");
					return;
				}
				if (!imageCode.toLowerCase().equals(imgCode)) {
					addFieldError("imageCode", "验证码输入错误");
					return;
				}
			}

		} catch (Exception e) {
			addFieldError("SERVER_ERROR", "未知错误");
			logger.error("" + e, e);
		}

	}

	public String execute() throws Exception {
		try {
			if (logger.isDebugEnabled()) {
				logger.debug("execute() - start"); //$NON-NLS-1$
			}

			String message = "未知错误";

		Agent cust = chiefService.updateAnnLogin(username,password,co);	
	//		Cust cust = mainService.custLogin(username, password);
			if (cust == null) {
				message = "用户名或密码错误";
				addFieldError("ERROR", message);
				return INPUT;
			} else if(Constant.Y.equalsIgnoreCase(cust.getAG_IS_LOCKED())){
				message = "该用户被锁定";
				addFieldError("ERROR", message);
				return INPUT;
			} else {
				session.put("customer", cust);
				this.addActionMessage("登录成功");
				{// 移除验证码session
					session.put(Constant.CAPTCHA_SESSION_HOLDER, "");
				}
				return SUCCESS;
			}
		} catch (Exception e) {
			logger.error("Customer login error:+" + e, e);
			throw new RuntimeException(e);
		}
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setImageCode(String imageCode) {
		this.imageCode = imageCode;
	}

	public String getImageCode() {
		return imageCode;
	}

	public void setChiefService(ChiefService chiefService) {
		this.chiefService = chiefService;
	}

	public ChiefService getChiefService() {
		return chiefService;
	}

	public void setCo(String co) {
		this.co = co;
	}

	public String getCo() {
		return co;
	}

}
